#include "StdAfx.h"
#include "WinBoardInputQueue.h"

using namespace Rorn::Chess;

WinBoardInputQueue::WinBoardInputQueue(void)
{
	::InitializeCriticalSection(&containerAccessCriticalSection_);
}

WinBoardInputQueue::~WinBoardInputQueue(void)
{
	::DeleteCriticalSection(&containerAccessCriticalSection_);
}

void WinBoardInputQueue::AddMessage(const std::string& message)
{
	::EnterCriticalSection(&containerAccessCriticalSection_);

	messageQueue_.push(message);

	::LeaveCriticalSection(&containerAccessCriticalSection_);
}

bool WinBoardInputQueue::HasMessages() const
{
	::EnterCriticalSection(&containerAccessCriticalSection_);

	bool hasMessages = (!messageQueue_.empty());

	::LeaveCriticalSection(&containerAccessCriticalSection_);

	return hasMessages;
}

void WinBoardInputQueue::GetNextMessage(std::string& message)
{
	::EnterCriticalSection(&containerAccessCriticalSection_);

	message = messageQueue_.front();
	messageQueue_.pop();

	::LeaveCriticalSection(&containerAccessCriticalSection_);
}